﻿using strange.extensions.mediation.impl;
using System;
using System.Collections.Generic;
using strange.extensions.dispatcher.eventdispatcher.api;
using UnityEngine;
using System.Collections;

public class CharacterMediator:EventMediator
{
    [Inject]
    public CharacterView CharacterView { get; set; }
    public override void OnRegister()
    {
        Init();
        dispatcher.AddListener(CommandEvent.Command_DealCard, OnDealCard);
        dispatcher.AddListener(ViewEvent.CompeletDeal, OnCompelet);
        dispatcher.AddListener(ViewEvent.DealThreeCard, OnDealThreeCard);
        dispatcher.AddListener(ViewEvent.RegisterPlay, OnPlayerRegisterPlayCard);
        dispatcher.AddListener(ViewEvent.SuccessedPlayCard, OnSuccessedPlayCard);
        dispatcher.AddListener(ViewEvent.GameSuccessed, OnGameSuccessed);
        dispatcher.AddListener(ViewEvent.ReStartGame, OnReStartGame);
        //注册事件
        RoundModel.ComputerHandler += RoundModel_ComputerHandler;

        dispatcher.Dispatch(CommandEvent.Command_UpdataIntegration);
        dispatcher.AddListener(ViewEvent.UpdataIntegration, OnGameSuccessed);
    }


    public override void OnRemove()
    {
        dispatcher.RemoveListener(CommandEvent.Command_DealCard, OnDealCard);
        dispatcher.RemoveListener(ViewEvent.CompeletDeal, OnCompelet);
        dispatcher.RemoveListener(ViewEvent.DealThreeCard, OnDealThreeCard);
        dispatcher.RemoveListener(ViewEvent.RegisterPlay, OnPlayerRegisterPlayCard);
        dispatcher.RemoveListener(ViewEvent.SuccessedPlayCard, OnSuccessedPlayCard);
        dispatcher.RemoveListener(ViewEvent.GameSuccessed, OnGameSuccessed);
        dispatcher.RemoveListener(ViewEvent.ReStartGame, OnReStartGame);
        dispatcher.RemoveListener(ViewEvent.UpdataIntegration, OnGameSuccessed);
        //注销事件
        RoundModel.ComputerHandler -= RoundModel_ComputerHandler;
    }

    /// <summary>
    /// 初始化角色
    /// </summary>
    private void Init()
    {
        CharacterView.Player.Identity = Identity.Farmer;
        CharacterView.LeftComputer.Identity = Identity.Farmer;
        CharacterView.RightComputer.Identity = Identity.Farmer;
    }
    #region 回调函数
    /// <summary>
    /// 发牌的回调
    /// </summary>
    /// <param name="evt">发牌的参数</param>
    private void OnDealCard(IEvent evt)
    {
        DealCardAgrs e = evt.data as DealCardAgrs;
        CharacterView.AddCard(e.cType, e.card, e.isSelected);
    }

    /// <summary>
    /// 完成发牌的回调
    /// </summary>
    /// <param name="payload"></param>
    private void OnCompelet()
    {
        CharacterView.Player.Sort(false);
        CharacterView.Desk.Sort(true);
        CharacterView.LeftComputer.Sort(true);
        CharacterView.RightComputer.Sort(true);
    }
    /// <summary>
    /// 抢地主的回调
    /// </summary>
    /// <param name="payload"></param>
    private void OnDealThreeCard(IEvent evt)
    {
        GrabAgrs e = evt.data as GrabAgrs;
        CharacterView.AddThreeCard(e.cType);
        //改变抢过地主后的状态
        CharacterView.Desk.IsGrabed = true;
    }
    /// <summary>
    /// 角色出牌的回调
    /// </summary>
    /// <param name="payload"></param>
    private void OnPlayerRegisterPlayCard()
    {
        CardType ct = CardType.None;
        List<Card> cards = CharacterView.Player.FindSelectCards();
        bool iscanPop = Rules.isCanPop(cards, out ct);
        if (ct != CardType.None&&iscanPop)//如果可以出牌
        {
            //把桌面上的牌清空
            CharacterView.Desk.ClearDesk();
            //添加派发的参数
            PlayCardArgs e = new PlayCardArgs()
            {
                CharacterType = CharacterType.Player,
                currentCardType = ct,
                currentWeight = Tools.GetWeight(cards, ct),
                currentLength = cards.Count
            };
    
            dispatcher.Dispatch(CommandEvent.Command_PlayCard, e);
            dispatcher.Dispatch(ViewEvent.CompletePlayCard, e);
            //判断是否还有牌
            if (!CharacterView.Player.HasCard)
            {
                //玩家胜利
                //右电脑胜利
                Identity p = CharacterView.Player.Identity;
                Identity l = CharacterView.LeftComputer.Identity;
                Identity r = CharacterView.RightComputer.Identity;
                CharacterType landlord;//谁是地主
                if (p == Identity.Landlord)
                {
                    landlord = CharacterType.Player;
                }
                else if (l == Identity.Landlord)
                {
                    landlord = CharacterType.ComputerLeft;
                }
                else
                {
                    landlord = CharacterType.ComputerRight;
                }
                GameOverAgrs eee = new GameOverAgrs()
                {
                    ComputerRightWIn = true,
                    PlayerWin = r == p ? true : false,
                    ComputerLeftWin = r == l ? true : false,
                    Loader = landlord
                };
                dispatcher.Dispatch(CommandEvent.Command_Successed, eee);
            }

        }
        else
        {
            Debug.Log("请选择正确的牌型！");
        }
    }

    /// <summary>
    /// 成功出牌的回调
    /// </summary>
    private void OnSuccessedPlayCard()
    {
        List<Card> cards = CharacterView.Player.FindSelectCards();

        //给桌面添加牌
        foreach (Card card in cards)
        {
            CharacterView.Desk.AddCard(card, false);
        }
         CharacterView.Desk.Sort(true);
        //可以出牌
        CharacterView.Player.DelateCardUI();

    }

    /// <summary>
    /// 电脑出牌的回调
    /// </summary>
    /// <param name="obj"></param>
    private void RoundModel_ComputerHandler(ComputerSmartAgrs e)
    {
        StartCoroutine("DelayPlayCard", e);
    }
    /// <summary>
    /// 延迟出牌
    /// </summary>
    /// <param name="e"></param>
    /// <returns></returns>
    IEnumerator DelayPlayCard(ComputerSmartAgrs e)
    {
        yield return new WaitForSeconds(0.5f);
        bool canPlay = false;
        switch (e.character)
        {
            case CharacterType.ComputerRight:
                canPlay = CharacterView.RightComputer.ComputerSmaterPlayCard(e.cardType, e.weight, e.length, e.biggestCharacter == CharacterType.ComputerRight);
                if (canPlay)
                {
                    List<Card> cards = CharacterView.RightComputer.SelectCards;
                    CardType ct = CharacterView.RightComputer.currentCardType;
                    //把桌面上的牌清空
                    CharacterView.Desk.ClearDesk();
                    //添加派发的参数
                    PlayCardArgs ee = new PlayCardArgs()
                    {
                        CharacterType = CharacterType.ComputerRight,
                        currentCardType = ct,
                        currentWeight = Tools.GetWeight(cards, ct),
                        currentLength = cards.Count
                    };
                    dispatcher.Dispatch(CommandEvent.Command_PlayCard, ee);

                    //给桌面添加牌
                    foreach (Card card in cards)
                    {
                        CharacterView.Desk.AddCard(card, false);
                    }
                    CharacterView.Desk.Sort(true);

                    dispatcher.Dispatch(ViewEvent.CompletePlayCard, ee);
                    //判断是否还有牌
                    if (!CharacterView.RightComputer.HasCard)
                    {
                        //右电脑胜利
                        Identity p = CharacterView.Player.Identity;
                        Identity l = CharacterView.LeftComputer.Identity;
                        Identity r = CharacterView.RightComputer.Identity;
                        CharacterType landlord;//谁是地主
                        if (p == Identity.Landlord)
                        {
                            landlord = CharacterType.Player;
                        }
                        else if (l == Identity.Landlord)
                        {
                            landlord = CharacterType.ComputerLeft;
                        }
                        else
                        {
                            landlord = CharacterType.ComputerRight;
                        }
                        GameOverAgrs eee = new GameOverAgrs()
                        {
                            ComputerRightWIn = true,
                            PlayerWin = r == p ? true : false,
                            ComputerLeftWin = r == l ? true : false,
                            Loader = landlord
                        };
                        dispatcher.Dispatch(CommandEvent.Command_Successed, eee);
                    }
                }
                else
                {
                    dispatcher.Dispatch(CommandEvent.Command_Pass);
                }
                break;
            case CharacterType.ComputerLeft:
                canPlay = CharacterView.LeftComputer.ComputerSmaterPlayCard(e.cardType, e.weight, e.length, e.biggestCharacter == CharacterType.ComputerLeft);
                if (canPlay)
                {
                    List<Card> cards = CharacterView.LeftComputer.SelectCards;
                    CardType ct = CharacterView.LeftComputer.currentCardType;
                    //把桌面上的牌清空
                    CharacterView.Desk.ClearDesk();
                    //添加派发的参数
                    PlayCardArgs ee = new PlayCardArgs()
                    {
                        CharacterType = CharacterType.ComputerLeft,
                        currentCardType = ct,
                        currentWeight = Tools.GetWeight(cards, ct),
                        currentLength = cards.Count
                    };
                    dispatcher.Dispatch(CommandEvent.Command_PlayCard, ee);

                    //给桌面添加牌
                    foreach (Card card in cards)
                    {
                        CharacterView.Desk.AddCard(card, false);
                    }
                    CharacterView.Desk.Sort(true);

                    dispatcher.Dispatch(ViewEvent.CompletePlayCard, ee);
                    //判断是否还有牌
                    if (!CharacterView.LeftComputer.HasCard)
                    {
                        //左电脑胜利
                        Identity p = CharacterView.Player.Identity;
                        Identity l = CharacterView.LeftComputer.Identity;
                        Identity r = CharacterView.RightComputer.Identity;
                        CharacterType landlord;//谁是地主
                        if (p == Identity.Landlord)
                        {
                            landlord = CharacterType.Player;
                        }
                        else if (l == Identity.Landlord)
                        {
                            landlord = CharacterType.ComputerLeft;
                        }
                        else
                        {
                            landlord = CharacterType.ComputerRight;
                        }

                        GameOverAgrs eee = new GameOverAgrs()
                        {
                            ComputerLeftWin = true,
                            PlayerWin = l == p ? true : false,
                            ComputerRightWIn = l == r ? true : false,
                            Loader = landlord
                        };
                        dispatcher.Dispatch(CommandEvent.Command_Successed, eee);
                    }
                }
                else
                {
                    dispatcher.Dispatch(CommandEvent.Command_Pass);
                }
                break;
            default:
                break;
        }
    }
    /// <summary>
    /// 游戏结束的回调
    /// </summary>
    /// <param name="evt"></param>
    private void OnGameSuccessed(IEvent evt)
    {
        // int result = (int)evt.data;
        GameData Gamedata =evt.data as GameData ;
        CharacterView.Player.characterUI.SetIntegration(Gamedata.PlayerIntegration);
        CharacterView.LeftComputer.characterUI.SetIntegration(Gamedata.ComputerLeftIntegration);
        CharacterView.RightComputer.characterUI.SetIntegration(Gamedata.ComputerRightIntegration);
        //
    }
    /// <summary>
    /// 重新开始
    /// </summary>
    private void OnReStartGame()
    {
        CharacterView.Player.CardList.Clear();
        CharacterView.LeftComputer.CardList.Clear();
        CharacterView.RightComputer.CardList.Clear();
        Init();
    }
    #endregion
}

